package manage.controller;

import manage.dao.UserMapper;
import manage.model.PeopleChange;
import manage.model.User;
import manage.service.impl.PeopleChangeServiceImpl;
import manage.util.LogUtil;
import manage.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
public class PeoPleChangeController {

    @Autowired
    private PeopleChangeServiceImpl peopleChangeService;

    @Autowired
    private UserMapper userMapper;

    private User user= null;

    //返回peopleChange的list
    @RequestMapping(value = "/peopleChange")
    @ResponseBody
    public Msg peopleChangeData(HttpServletRequest request, Authentication authentication){
        String page = request.getParameter("currentPage");
        String limit = request.getParameter("pageSize");
        UserDetails userDetails =(UserDetails) authentication.getPrincipal();
        user = userMapper.selectByPrimaryKey(userDetails.getUsername());
        return Msg.success().add("data",peopleChangeService.selectAll(page,limit));
    }

    //添加peopleChange
    @ResponseBody
    @RequestMapping(value = "/addPeopleChange",method = RequestMethod.POST)
    public Msg addPeopleChange(PeopleChange peopleChange){
        peopleChange.setOperatorId(user.getAccount());
        peopleChange.setOperatDate(new Date());
        LogUtil.writeFileLog(user.getAccount(),"添加人员变动:"+peopleChange.toString());
        String s= peopleChangeService.insert(peopleChange);
        return Msg.attendanceAdd(s);
    }

    //修改peopleChange
    @ResponseBody
    @RequestMapping(value = "/addPeopleChange",method = RequestMethod.PUT)
    public Msg updatePeopleChange(PeopleChange peopleChange){
        peopleChange.setOperatDate(new Date());
        peopleChange.setOperatorId(user.getAccount());
        LogUtil.writeFileLog(user.getAccount(),"修改人员变动-改:"+peopleChange.toString());
        String s= peopleChangeService.update(peopleChange);
        return Msg.attendanceAdd(s);
    }
    //删除peopleChange
    @ResponseBody
    @RequestMapping("/delPeopleChange")
    public Msg delPeopleChange(@RequestBody Integer id){
        String s = peopleChangeService.del(id);
        LogUtil.writeFileLog(user.getAccount(),"删除人员变动:"+id.toString());
        return Msg.attendanceAdd(s);
    }

    //根据主键查peopleChange 和修改配合使用
    @ResponseBody
    @RequestMapping("/peopleChangeSelect")
    public Msg selPeopleChange(@RequestBody Integer id){
        PeopleChange peopleChange = peopleChangeService.select(id);
        LogUtil.writeFileLog(user.getAccount(),"修改人员变动-查:"+peopleChange.toString());
        return Msg.success().add("peopleChange",peopleChange);
    }
}